package com.artogrid.service.impl;

import java.io.Serializable;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.MessageCreator;

import com.artogrid.service.IServiceTest;

public class ServiceTest implements IServiceTest {
	
	private JmsOperations jmsTemplate;
	
	private Destination destination;
	
	public void setDestination(Destination destination) {
		this.destination = destination;
	}


	public void setJmsTemplate(JmsOperations jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	@Override
	public boolean sendMessage(String content) {
		return sendTopic(content);
	}

	private boolean sendTopic(String sendMessage) {
		final String send = sendMessage;
		jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return (Message) session.createTextMessage(send);
			}
		});
		return true;
	}

	@Override
	public boolean sendObjectMessage(final Serializable so) {
		jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return (ObjectMessage) session.createObjectMessage(so);
			}
		});
		return true;
	}

}
